package com.jiuyou.storeController.mall.bbs;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.shopping.domain.BbsLike;
import com.ruoyi.shopping.service.IBbsLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chengliang
 * @date 2022/6/20
 **/
@RestController
@RequestMapping("/store/mall/bbs/options")
public class MallBBSOptionsStoreController extends StoreBaseController {

    @Autowired
    private IBbsLikeService likeService;


    @PostMapping("/add")
    public AjaxResult add(BbsLike like){
        Long storeUserId=getLoginStoreUserId();
        like.setCreateBy(storeUserId);
        like.setBelongs(DicConstants.BUSINESS_Business.getCode());
        likeService.insertBbsLike(like);
        return AjaxResult.success();
    }
    @PostMapping("del")
    public AjaxResult del(BbsLike like){
        Long storeUserId=getLoginStoreUserId();
        like.setCreateBy(storeUserId);
        Long id = likeService.selectBbsLikeList(like).get(0).getId();
        likeService.deleteBbsLikeById(id);
        return AjaxResult.success();
    }
    @PostMapping("/getMineData/{bbsId}")
    public AjaxResult getMineData(@PathVariable Long bbsId){
        Long storeUserId=getLoginStoreUserId();
        BbsLike bbsLike = new BbsLike();
        bbsLike.setForeignId(bbsId);
        bbsLike.setCreateBy(storeUserId);
        List<BbsLike> bbsLikes = likeService.selectBbsLikeList(bbsLike);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("like",false);
        jsonObject.put("collect",false);
        for (BbsLike like : bbsLikes) {
            if(DicConstants.BBS_OPTIOPN_LIKE.getCode().equals(like.getBelongType())){
                jsonObject.put("like",true);
            }
            if(DicConstants.BBS_OPTIOPN_COLLECT.getCode().equals(like.getBelongType())){
                jsonObject.put("collect",true);
            }
        }
        return AjaxResult.success(jsonObject);
    }
    @PostMapping("/getOptionsCount/{id}")
    public AjaxResult getLikeCount(@PathVariable Long id){
        BbsLike bbsLike = new BbsLike();
        bbsLike.setBelongType(DicConstants.BBS_OPTIOPN_COLLECT.getCode());
        bbsLike.setForeignId(id);
        Integer collectCount = likeService.selectCount(bbsLike);
        bbsLike.setBelongType(DicConstants.BBS_OPTIOPN_LIKE.getCode());
        Integer likeCount = likeService.selectCount(bbsLike);
        JSONObject o = new JSONObject();
        o.put("collectCount",collectCount);
        o.put("likeCount",likeCount);
        return AjaxResult.success(o);
    }
}
